package com.ima.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ima.web.beans.db.Roles;
import com.ima.web.beans.db.UserRoles;
import com.ima.web.beans.db.Users;
import com.ima.web.beans.form.UsersForm;
import com.ima.web.dao.UserRolesDao;
import com.ima.web.dao.UsersDao;
import com.ima.web.dao.impl.UsersDaoImpl;
import com.ima.web.exceptions.UserExistsException;
import com.ima.web.service.RolesService;
import com.ima.web.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService{

	private UsersDao usersDao;
	private UserRolesDao userRolesDao;
	private RolesService rolesService;
	private Logger log;
	
	public UserServiceImpl(){
		log = Logger.getLogger(this.getClass());
	}
	
	@Autowired
	public void setDependencies(UsersDao usersDao,UserRolesDao userRolesDao,RolesService rolesService){
		this.usersDao = usersDao;
		this.userRolesDao = userRolesDao;
		this.rolesService = rolesService;
	}
	
	@Override
	public Users getUserData(int id) {
		return usersDao.findById(id);
	}

	@Override
	public Users getUserData(String username) {
		List<Users> u =usersDao.list(new Users(username));
		
		if(u.size()>0)
			return u.get(0);
		else 
			return null;
	}

	@Override
	public Users createUser(Users u, Roles role) throws UserExistsException {
		List<Roles> r = new ArrayList<Roles>();
		r.add(role);
		
		return createUser(u,r);
	}

	@Override
	public boolean checkUserExists(String username) {
		if(usersDao.list(new Users(username)).size()>0)
			return true;
		else
			return false;
	}

	@Override
	public Users signUpUser(UsersForm u) throws UserExistsException {
		Users nu = new Users();
		nu.setPenName(u.getPenName());
		nu.setUsername(u.getUsername());
		//TODO: encrypt password
		PasswordEncoder pe = new ShaPasswordEncoder();
		nu.setPassword(pe.encodePassword(u.getPassword(),null));
		nu.setEmail(u.getEmail());
		nu.setEmail2(u.getEmail2());
		
		
		System.out.println("password: "+pe.encodePassword(u.getPassword(),null));
		
		return createUser(nu,rolesService.getRoleById(Integer.parseInt(u.getRolesId())));
	}

	@Override
	public Users createUser(Users u, List<Roles> r) throws UserExistsException {
		if(!checkUserExists(u.getUsername())){
			usersDao.save(u);
			
			for(Roles role : r){
				UserRoles ur = new UserRoles();
				
				ur.setRoles(role);
				ur.setUserId(u.getUsersId());
				ur.setUsername(u.getUsername());
				
				userRolesDao.save(ur);
			}
		}else{
			throw new UserExistsException(u.getUsername());
		}
		
		return u;
	}

	

	

}
